using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using TXLooker.Common;
using TXLooker.WorkItemExtention.Core;


namespace TXLooker.WorkItemExtention.UI.OLViewExtension
{
    public partial class OLServerViewControl : UserControl
    {
        private int PagesCount = 1;
        private const int LinksPerPage = 26;
        private readonly List<WIObject> projects;
        private readonly Uri serverUri;

        private int PagesNumber
        {
            get
            {
                return (int)Math.Ceiling((double)projects.Count / LinksPerPage);
            }
        }

        private String SecondLabelText
        {
            get
            {
                return String.Format(" of {0}", PagesNumber);
            }
        }

        public OLServerViewControl(Uri ServerUri, List<WIObject> Projects)
        {
            InitializeComponent();
            serverUri = ServerUri;
            projects = Projects;
            projects.Sort();
            InitializeControls();
        }

        private void InitializeControls()
        {
            if (projects.Count > LinksPerPage)
            {
                Application.EnableVisualStyles();
                // buttons
                btnNext.Visible = true;
                btnNextBottom.Visible = true;
                btnPrevious.Visible = true;
                btnPreviousBottom.Visible = true;
                btnPrevious.Enabled = false;
                btnPreviousBottom.Enabled = false;
                // labales
                lblPagesCount.Visible = true;
                lblPagesCountBottom.Visible = true;
                lblOfPages.Visible = true;
                lblOfPagesBottom.Visible = true;
                lblOfPages.Text = SecondLabelText;
                lblOfPagesBottom.Text = SecondLabelText;
                lblPagesCount.Text = PagesCount.ToString();
                lblPagesCountBottom.Text = PagesCount.ToString();
            }

            lblServerName.Text = serverUri.AbsoluteUri;

            InitializeLinksForPage(PagesCount);
            SetControlsPosition();
        }

        private void InitializeLinksForPage(int pagesCount)
        {
            lnkProjects.Text = String.Empty;
            lnkProjects.Links.Clear();
            String projectName;
            int to = projects.Count < pagesCount * LinksPerPage ? projects.Count : pagesCount * LinksPerPage;

            for (int i = (pagesCount - 1) * LinksPerPage; i < to; i++)
            {
                try
                {
                    lnkProjects.Text += String.Format("{0}\n", projects[i].FolderName);
                    projectName = projects[i].FolderName.Replace("Project: ", String.Empty);
                    lnkProjects.Links.Add(lnkProjects.Text.Length - projectName.Length - 1, projectName.Length, projects[i].Folder.EntryID);
                }
                catch (Exception ex)
                {
                    LogManager.WriteMessageToCustomLog(ex);
                    lnkProjects.Links.Clear();
                    lnkProjects.Text = "Error occured";
                    break;
                }
            }
        }

        private void DockedControl_Load(object sender, EventArgs e)
        {
            BackColor = Color.White;
        }

        private void logoContainer_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("www.teamexpand.com");
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            StepBackward();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            StepForward();
        }

        private void btnBottomPrevious_Click(object sender, EventArgs e)
        {
            StepBackward();
        }

        private void btnBottomNext_Click(object sender, EventArgs e)
        {
            StepForward();
        }

        private void StepBackward()
        {
            btnPrevious.Enabled = true;
            PagesCount--;
            lblPagesCount.Text = PagesCount.ToString();
            lblPagesCountBottom.Text = PagesCount.ToString();
            InitializeLinksForPage(PagesCount);
            SetControlsPosition();
            if (PagesCount <= 1)
            {
                btnPrevious.Enabled = false;
                btnPreviousBottom.Enabled = false;
            }

            if (PagesCount < PagesNumber)
            {
                btnNext.Enabled = true;
                btnNextBottom.Enabled = true;
            }
        }

        private void StepForward()
        {
            btnPrevious.Enabled = true;
            btnPreviousBottom.Enabled = true;
            PagesCount++;
            lblPagesCount.Text = PagesCount.ToString();
            lblPagesCountBottom.Text = PagesCount.ToString();
            InitializeLinksForPage(PagesCount);
            SetControlsPosition();
            if (PagesCount >= PagesNumber)
            {
                btnNext.Enabled = false;
                btnNextBottom.Enabled = false;
            }
        }

        private void SetControlsPosition()
        {
            //var newY = lnkProjects.Height + btnBottomPrevious.Height;

            //btnBottomPrevious.Location = new Point(btnPrevious.Location.X, newY);
            //btnBottomNext.Location = new Point(btnNext.Location.X, newY);
            //panelButtons.Location = new Point(panelButtons.Location.X, newY);
        }
    }
}